package com.traffic.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;

import com.traffic.exception.BaseException;
import com.traffic.model.ApiResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
//import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.traffic.entity.Feedtype;
import com.traffic.service.FeedtypeService;

import java.util.*;


@Api(tags = "反馈相关接口")
@RestController
@RequestMapping(value = "/feedtypes")
@Slf4j
public class FeedtypeController {
    
    @Autowired
    private FeedtypeService feedtypeService;

    @ApiOperation(value = "【反馈类型】", notes = "反馈类型列表")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "page", value = "页面", required = true)
    @GetMapping("/all/{page}")
    public ApiResponse FeedtypePage(@PathVariable Integer page) {

        Integer count = feedtypeService.count();

        IPage<Feedtype> feedtypeIPage = new Page<Feedtype>(page, 10,count);
        //  IPage<Feedtype> feedtypes = feedtypeService.page(feedtypeIPage);
        //时间倒序列表查询

        QueryWrapper<Feedtype> wrapper = new QueryWrapper<>();
        IPage<Feedtype> feedtypes = feedtypeService.page(feedtypeIPage,wrapper.orderByDesc("createtime"));

        return ApiResponse.ofStatus(Status.OK, feedtypes);

    }


    @ApiOperation(value = "【反馈类型】", notes = "选择反馈类型全部列表")
    @GetMapping("/alltype")
    public ApiResponse AllFeedtype() {
        return ApiResponse.ofStatus(Status.OK,feedtypeService.list());

    }

    @ApiOperation(value = "【反馈类型】", notes = "批量删除反馈类型")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "deleteId", value = "删除反馈的id", required = true)
    @PostMapping(value = "/multidelete")
    public ApiResponse AllDeleteFeedtype(@RequestBody List<Integer> ids) {

        feedtypeService.removeByIds(ids);

        return ApiResponse.ofStatus(Status.OK);
    }

    @ApiOperation(value = "【反馈类型】", notes = "删除反馈类型")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "deleteId", value = "删除反馈的id", required = true)
    @DeleteMapping(value = "/delete/")
    public ApiResponse deleteFeedtype(@PathVariable Long deleteId) {

        feedtypeService.removeById(deleteId);

        return ApiResponse.ofStatus(Status.OK);
    }


    @ApiOperation(value = "【反馈】", notes = "新增反馈")
    @PostMapping("/insert")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =String.class,key = "type",example = "类型IV" , description = "反馈类型"),

    })
    public ApiResponse insertFeedtype(@RequestBody Map<String, String> feedtype) {




        String param[] = new String[]{"name"};

        List<String> paramList = CollUtil.newArrayList(param);
        if (!paramList.containsAll(CollUtil.newArrayList(feedtype.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Feedtype fed = new Feedtype();
        BeanUtil.copyProperties(feedtype, fed, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        fed.setCreatetime(new DateTime());
        feedtypeService.save(fed);

        return ApiResponse.ofStatus(Status.OK);
    }
    
}
